<?php

/**
 * 评论
 * Class commit
 */
class commit {

    public function  __construct() {
        $this->_auth = param::get_cookie('auth');

        if (!$this->_auth) {
            json_exit('请先登录', 201);
        }
    }

    //评论
    public function add() {
        $uid = (int)param::get_cookie('_userid');
        $catid = $_POST['catid'] ? (int)$_POST['catid'] : 0;
        $id = $_POST['id'] ? (int)$_POST['id'] : 0;
        $star = $_POST['star'] ? (int)$_POST['star'] : 5;
        $content = $_POST['content'] ? safe_replace($_POST['content']) : '';
        $ymd = date('Y-m-d');
        $db_commit = pc_base::load_model('commit_model');

        //查是否已有
        $sql = "uid='$uid' and ymd='$ymd'";
        $res = $db_commit->get_one($sql);

        if (!$res) {
            //加分
            $db_member = pc_base::load_model('member_model');
            $cha = $db_member::log($uid, 30, $db_member::TYPE_COMMIT, "评论-$catid-$id", __METHOD__);
            $score = 30;
        } else {
            $score = 0;
        }

        //记录
        $db_commit->insert([
            'uid' => $uid,
            'a_catid' => $catid,
            'a_id' => $id,
            'star' => $star,
            'content' => $content,
            'ymd' => date('Y-m-d'),
            'ymdhis' => date('Y-m-d H:i:s'),
        ]);

        json_exit($score, 100, '评论成功');
    }

}